<?php
include_once ("classes/classes.php");
include_once ("includes/site_config.php");
session_save_path($session_path);
session_start();
include_once ("includes/dbconnect.php");
include_once ("includes/utils.php");
include_once ("includes/team_utils.php");

setlocale(LC_ALL, $locale);
$loggedin = getSession("loggedin");
if (!isset($loggedin) && isset($_COOKIE["coach"])) {
	$loggedin = loadCoach($_COOKIE["coach"]);
    $_SESSION["loggedin"] = $loggedin;
}

$skills = getSession("skills");
$ruleSets = getSession("ruleSets");
$races = getSession("races");
$seasons = getSession("seasons");
$activeSeasons = getSession("activeSeasons");
$season = getSession("season");
$teams = getSession("teams");
$matches = getSession("matches");

$reload = getParam("reload");
if (isset ($reload)) {
    $reload = true;
} else {
    $reload = false;
}
$sd = new SeasonDao();
if (!isset ($races) or $reload) {
    $skills = loadSkills();
	$_SESSION["skills"] = $skills;

    $rsDao = new RuleSetDao();
    $ruleSets = $rsDao->loadRuleSets();
	$_SESSION["ruleSets"] = $ruleSets;

    $races = loadRaces();
	$_SESSION["races"] = $races;

    $seasons = $sd->loadSeasons();
	$_SESSION["seasons"] = $seasons;

    $activeSeasons = $sd->loadActiveSeasons();
	$_SESSION["activeSeasons"] = $activeSeasons;
}

$season_id = getParam("season_id");
if (isset ($season_id) and (!isset ($season) or $season->id != $season_id)) {
    $season = new Season($season_id);
    $_SESSION["season"] = $season;
    $reload = true;
} else
    if (!isset ($season)) {
        $season = new Season($defaultSeason);
	    $_SESSION["season"] = $season;
        $reload = true;
    }

if (!isset ($teams) or $reload) {
    $teams = new Teams();
    $md = new MatchDao();
    $matches = $md->findSeasonMatches($season);
	$_SESSION["teams"] = $teams;
	$_SESSION["matches"] = $matches;
}
?>